package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username=scan.nextLine();
        System.out.println("请输入密码");
        String password=scan.nextLine();
//        获取连接
        try ( Connection conn =DBUtils.getCoon()){
//            Statement s =conn.createStatement();
////            准备登录SQL语句
//            String sql="select count(*) from user where "
//                    +"username='"+username+"' and password='"+password+"'";
////            执行查询
//            ResultSet rs= s.executeQuery(sql);
            String sql="select count(*) from user where "
                    +"username=? and password=?";
            //            创建带有预编译功能的 执行SQL语句对象
            PreparedStatement ps= conn.prepareStatement(sql);
            //            替换SQL语句中的？
            ps.setString(1,username);
            ps.setString(2,password);

            ResultSet rs=ps.executeQuery();
//            游标下移，指向查询到的数量
            rs.next();
            int count =rs.getInt(1);
//            判断大于0 说明存在即登录成功 反之失败
            if(count>0){
                System.out.println("登录成功");
            }else {
                System.out.println("用户名或密码错误！");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}
